package zcw.com.lib_leet_code;

import zcw.com.lib_leet_code.util.Util;

/**
 * Created by 朱城委 on 2021/7/13.<br><br>
 *
 * 移动零
 */
public class Topic283 {
    public static void main(String[] args) {
        Topic283 instance = new Topic283();

        int[] array = new int[] {0,1,0,3,12};
        instance.moveZeroes(array);
        Util.printArray(array);

        array = new int[] {0};
        instance.moveZeroes(array);
        Util.printArray(array);
    }

    public void moveZeroes(int[] nums) {
        int zeroIndex = 0;
        for(int i = 0; i < nums.length; i++) {
            if(nums[i] != 0) {
                nums[zeroIndex] = nums[i];
                zeroIndex++;
            }
        }

        for(int i = zeroIndex; i < nums.length; i++) {
            nums[i] = 0;
        }
    }

    public void moveZeroes2(int[] nums) {
        int zeroIndex = nums.length - 1;
        int i = 0;
        while (i < zeroIndex) {
            if(nums[i] == 0) {
                for(int j = i + 1; j <= zeroIndex; j++) {
                    nums[j - 1] = nums[j];
                }
                nums[zeroIndex] = 0;
                zeroIndex--;
            }
            else {
                i++;
            }
        }
    }
}
